package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.LossItem;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.LossItemQueryObject;
import cn.wolfcode.crm.service.ILossItemService;
import cn.wolfcode.crm.util.PermissionName;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("lossItem")
public class LossItemController {

    @Autowired
    private ILossItemService lossItemService;

    @RequestMapping("")
    @RequiresPermissions("lossItem:index")
    @PermissionName("事故明细列表")
    public String index() {
        //只负责跳转到lossItem.jsp页面即可
        return "lossItem";
    }

    @RequestMapping("list")
    @RequiresPermissions("lossItem:list")
    @PermissionName("事故明细数据")
    @ResponseBody
    public PageResult list(LossItemQueryObject qo) {
        PageResult result = lossItemService.query(qo);
        return result;
    }
    //导出表格
    @RequestMapping("export")
    @ResponseBody
    public void export(LossItemQueryObject qo, HttpServletResponse response) throws Exception {
        //1.查询出列表数据
        qo.setRows(10000);
        PageResult result = lossItemService.query(qo);
        List items = result.getRows();

        response.setHeader("Content-Disposition", "attachment;filename=lossItem.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标是 outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //2.处理标题
        //创建一个可以写 sheet ,
        WritableSheet sheet = workbook.createSheet("lossItem", 0);

        //创建一个单元格
        Label adminLabel = new Label(0, 0, "发生时间");
        Label adminLabe2 = new Label(1, 0, "事故地点");
        Label adminLabe3 = new Label(2, 0, "接通员工");
        Label adminLabe4 = new Label(3, 0, "处理时间");
        Label adminLabe5 = new Label(4, 0, "处理员工");
        Label adminLabe6 = new Label(5, 0, "车名");
        Label adminLabe7 = new Label(6, 0, "车主");
        Label adminLabe8 = new Label(7, 0, "保单编号");
        Label adminLabe9 = new Label(8, 0, "保单种类");
        Label adminLabel0 = new Label(9, 0, "事故描述");
        Label adminLabel1 = new Label(10, 0, "责任认定");
        Label adminLabel2 = new Label(11, 0, "总数量");
        Label adminLabel3 = new Label(12, 0, "总金额");
        Label adminLabel4 = new Label(13, 0, "卡号");
        Label adminLabel5 = new Label(14, 0, "保险公司");
        sheet.addCell(adminLabel);
        sheet.addCell(adminLabe2);
        sheet.addCell(adminLabe3);
        sheet.addCell(adminLabe4);
        sheet.addCell(adminLabe5);
        sheet.addCell(adminLabe6);
        sheet.addCell(adminLabe7);
        sheet.addCell(adminLabe8);
        sheet.addCell(adminLabe9);
        sheet.addCell(adminLabel0);
        sheet.addCell(adminLabel1);
        sheet.addCell(adminLabel2);
        sheet.addCell(adminLabel3);
        sheet.addCell(adminLabel4);
        sheet.addCell(adminLabel5);
        //3.循环迭代list,item 对应表格中的一个行数据,二每一个employe的每一个字段,应该对应一行的某一个单元格
        for(int i = 0; i < items.size() ;i++){
            LossItem item  = (LossItem) items.get(i);
            int row = i+1;
            Label label1 = null;
            Label label2 = null;
            Label label3 = null;
            Label label4 = null;
            Label label5 = null;
            Label label6 = null;
            Label label7 = null;
            Label label8 = null;
            Label label9 = null;
            Label label10 = null;
            Label label11 = null;
            Label label12 = null;
            Label label13 = null;
            Label label14 = null;
            Label label15 = null;

                label1 = new Label(0, row, item.getCasesTime().toLocaleString());
                label2 = new Label(1, row, item.getLossAddress());
                label3 = new Label(2, row, item.getCasesEmpName());
                label4 = new Label(3, row, item.getAccidentTime().toLocaleString());
                label5 = new Label(4, row, item.getAccidentEmpName());
                label6 = new Label(5, row, item.getCarName());
                label7 = new Label(6, row, item.getOwnerName());
                label8 = new Label(7, row, item.getPolicySn());
                label9 = new Label(8, row, item.getPolicyName());
                label10 = new Label(9, row, item.getCasesDescription());
                label11 = new Label(10, row, item.getResponsibility());
                label12 = new Label(11, row, item.getTotalNumber().toString());
                label13 = new Label(12, row, item.getTotalAccount().toString());
                label14 = new Label(13, row, item.getBankcard());
                label15 = new Label(14, row, item.getPolicyCompany());

            sheet.addCell(label1);
            sheet.addCell(label2);
            sheet.addCell(label3);
            sheet.addCell(label4);
            sheet.addCell(label5);
            sheet.addCell(label6);
            sheet.addCell(label7);
            sheet.addCell(label8);
            sheet.addCell(label9);
            sheet.addCell(label10);
            sheet.addCell(label11);
            sheet.addCell(label12);
            sheet.addCell(label13);
            sheet.addCell(label14);
            sheet.addCell(label15);
        }
        //4.下文件
        //把内容中的工作簿对象的相关内容写到 输出流 里面
        workbook.write();
        //关闭工作簿
        workbook.close();
    }

}


